<?php
/*
 * 账户统计
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\facade\Db;
use think\facade\Session;

class Statistics extends AdminBase
{

	//账目统计
    public function account()
    {
        return $this->fetch('');
    }
    //订单统计
    public function order()
    {
        return $this->fetch('');
    }
    //供应商统计
    public function supplier()
    {
        return $this->fetch('');
    }
    //时时费率
    public function rate()
    {
        return $this->fetch('');
    }
    //会员分析
    public function user()
    {
        return $this->fetch('');
    }
    //理财通分析
    public function deposit()
    {
        return $this->fetch('');
    }
    //理财通分析
    public function get_deposit()
    {
        //用户投资金额占比
        $poor = Db::table('deposit')->whereBetween('money',"0,500")->count('id');//贫困
        $wellOff = Db::table('deposit')->whereBetween('money',"501,5000")->count('id');//小康
        $prosperity = Db::table('deposit')->where('money','>',5001)->count('id');//富裕
        $deposit = [
            [
                'name' => "贫困",
                'value' => $poor
            ],[
                'name' => "小康",
                'value' => $wellOff
            ],[
                'name' => "富裕",
                'value' => $prosperity
            ]
        ];
        //收支明细
        $log = Db::table('deposit_statistics')->order('create_time','ASC')->select()->toArray();
        foreach ($log as $key => $v) {
            $date[] = $v['date'];
            $buy[] = $v['buy'];
            $sell[] = $v['sell'];
            $surplus[] = $v['surplus'];
        }
        $data['date'] = $date;
        $data['buy'] = $buy;
        $data['sell'] = $sell;
        $data['surplus'] = $surplus;
        return json_encode([$deposit,$data]);
    }
    //会员账目统计
    public function get_account()
    {
        $rs = Db::table('user')->field('sum(money) as money,sum(growth) as growth,sum(integral) as integral')->select()->toArray();
        $data['account'] = [
            [
                'value' =>   $rs[0]['money'],
                'name'  =>   '余额'
            ],[
                'value' =>   $rs[0]['growth'],
                'name'  =>   '成长值',
            ],[
                'value' =>   $rs[0]['integral'],
                'name'  =>   '积分',
            ]
        ];
        //收支明细
        $log = Db::table('account_log')->select()->toArray();
        $income = $date = $expend = 0;
        foreach ($log as $key => $v) {
            $income[] = $v['income_money'];
            $date[] = $v['date'];
            $expend[] = $v['expend_money'];
        }
        $data['income'] = $income;
        $data['date'] = $date;
        $data['expend'] = $expend;
        return json_encode([$data]);
    }

    //获取会员占比情况
    public function get_user_stats_info()
    {
        $sex = [];
        $age = [];
        $credit = [];
        $unknown = Db::table('user')->where('sex',0)->count('id');
        $man = Db::table('user')->where('sex',1)->count('id');
        $women = Db::table('user')->where('sex',2)->count('id');

        $child = Db::table('user')->whereBetween('age',"0,17")->count('id');
        $juvenile = Db::table('user')->whereBetween('age',"18,25")->count('id');
        $youth = Db::table('user')->whereBetween('age',"26,35")->count('id');
        $women = Db::table('user')->whereBetween('age',"36,45")->count('id');
        $middle = Db::table('user')->whereBetween('age',"46,60")->count('id');
        $old = Db::table('user')->whereBetween('age',"61,75")->count('id');
        $aging = Db::table('user')->where('age','>',75)->count('id');

        $poor = Db::table('user')->whereBetween('credit',"0,550")->count('id');
        $middling = Db::table('user')->whereBetween('credit',"551,650")->count('id');
        $good = Db::table('user')->whereBetween('credit',"651,700")->count('id');
        $outstanding = Db::table('user')->whereBetween('credit',"701,950")->count('id');
        $excellent = Db::table('user')->where('credit','>',950)->count('id');
        $sex = [
            [
                'name' => "保密",
                'value' => $unknown
            ],[
                'name' => "男",
                'value' => $man
            ],[
                'name' => "女",
                'value' => $women
            ]
        ];
        $age = [
            [
                'name' => "童年",
                'value' => $child
            ],[
                'name' => "少年",
                'value' => $juvenile
            ],[
                'name' => "青年",
                'value' => $youth
            ],[
                'name' => "壮年",
                'value' => $women
            ],[
                'name' => "中年",
                'value' => $middle
            ],[
                'name' => "老年",
                'value' => $old
            ],[
                'name' => "古稀",
                'value' => $aging
            ]
        ];
        $credit = [
            [
                'name' => "较差",
                'value' => $poor
            ],[
                'name' => "中等",
                'value' => $middling
            ],[
                'name' => "良好",
                'value' => $good
            ],[
                'name' => "优秀",
                'value' => $outstanding
            ],[
                'name' => "极好",
                'value' => $excellent
            ]
        ];
        return json_encode([$sex,$age,$credit]);
    }
}
